package com.itheima.web.service.impl.cms;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.service.cms.ICmsSubjectService;
import com.itheima.utils.MyPage;
import com.itheima.vo.cms.CmsSubject;
import com.itheima.web.mapper.cms.CmsSubjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 专题表 服务实现类
 * </p>
 *
 * @author 王志斌
 * @since 2022-07-06
 */
@DubboService
public class CmsSubjectServiceImpl extends ServiceImpl<CmsSubjectMapper, CmsSubject> implements ICmsSubjectService {
@Autowired
private CmsSubjectMapper cmsSubjectMapper;
    /**
     * 获取全部商品专题
     * @return
     */
    @Override
    public List<CmsSubject> listAll() {
        List<CmsSubject> cmsSubjects= cmsSubjectMapper.getAll();
        if (CollectionUtils.isEmpty(cmsSubjects)){
            return new ArrayList<>();
        }
        return cmsSubjects;
    }

    @Override
    public MyPage<CmsSubject> listpage(Long pageNum, Long pageSize, String keyword) {
        Page<CmsSubject> page = getListByPage(pageNum, pageSize, keyword);
        return MyPage.init(page);

    }

    public Page<CmsSubject> getListByPage(Long pageNum, Long pageSize, String keyword) {
        Page<CmsSubject> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CmsSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(keyword), CmsSubject::getCategoryName, keyword);
        //return cmsSubjectMapper.selectPage(page, wrapper);
        return this.page(page,wrapper);
    }
}
